<?php

namespace App\Hospital\Metrics\Examples;

use App\Models\HospitalUserFamily;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SignTotle extends Line
{
    /**
     * 初始化卡片内容
     *
     * @return void
     */
    protected function init()
    {
        parent::init();

        $this->title('签约居民');
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $sign = HospitalUserFamily::query()->where('hospital_id',Admin::user()->hospital_id)
            ->where('status',1)
            ->count();
        // 卡片内容
        $this->withContent($sign);
        $signDay = HospitalUserFamily::query()->where('hospital_id',Admin::user()->hospital_id)
            ->select(['starttime',DB::raw('count(*) as count')])
            ->where('status',1)
            ->groupBy('starttime')
            ->orderBy('starttime','desc')
            ->pluck('count')
            ->toArray();
        // 图表数据
        $this->withChart(array_reverse($signDay));
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => [
                [
                    'name' => $this->title,
                    'data' => $data,
                ],
            ],
        ]);
    }

    /**
     * 设置卡片内容.
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
    <h2 class="ml-1 font-lg-1">{$content}</h2>
    <span class="mb-0 mr-1 text-80">{$this->title}</span>
</div>
HTML
        );
    }
}
